// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.Aggregations;

internal sealed class DateRangeAggregationConverter : JsonConverter<DateRangeAggregation>
{
	public override DateRangeAggregation Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		if (reader.TokenType != JsonTokenType.StartObject)
			throw new JsonException("Unexpected JSON detected.");
		reader.Read();
		var aggName = reader.GetString();
		if (aggName != "date_range")
			throw new JsonException("Unexpected JSON detected.");
		var agg = new DateRangeAggregation(aggName);
		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				if (reader.ValueTextEquals("field"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Serverless.Field?>(ref reader, options);
					if (value is not null)
					{
						agg.Field = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("format"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<string?>(ref reader, options);
					if (value is not null)
					{
						agg.Format = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("missing"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<FieldValue?>(ref reader, options);
					if (value is not null)
					{
						agg.Missing = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("ranges"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeExpression>?>(ref reader, options);
					if (value is not null)
					{
						agg.Ranges = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("time_zone"))
				{
					reader.Read();
					var value = JsonSerializer.Deserialize<string?>(ref reader, options);
					if (value is not null)
					{
						agg.TimeZone = value;
					}

					continue;
				}
			}
		}

		while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
		{
			if (reader.TokenType == JsonTokenType.PropertyName)
			{
				if (reader.ValueTextEquals("meta"))
				{
					var value = JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options);
					if (value is not null)
					{
						agg.Meta = value;
					}

					continue;
				}

				if (reader.ValueTextEquals("aggs") || reader.ValueTextEquals("aggregations"))
				{
					var value = JsonSerializer.Deserialize<AggregationDictionary>(ref reader, options);
					if (value is not null)
					{
						agg.Aggregations = value;
					}

					continue;
				}
			}
		}

		return agg;
	}

	public override void Write(Utf8JsonWriter writer, DateRangeAggregation value, JsonSerializerOptions options)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("date_range");
		writer.WriteStartObject();
		if (value.Field is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, value.Field, options);
		}

		if (!string.IsNullOrEmpty(value.Format))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(value.Format);
		}

		if (value.Missing is not null)
		{
			writer.WritePropertyName("missing");
			JsonSerializer.Serialize(writer, value.Missing, options);
		}

		if (value.Ranges is not null)
		{
			writer.WritePropertyName("ranges");
			JsonSerializer.Serialize(writer, value.Ranges, options);
		}

		if (value.TimeZone is not null)
		{
			writer.WritePropertyName("time_zone");
			JsonSerializer.Serialize(writer, value.TimeZone, options);
		}

		writer.WriteEndObject();
		if (value.Meta is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, value.Meta, options);
		}

		if (value.Aggregations is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, value.Aggregations, options);
		}

		writer.WriteEndObject();
	}
}

[JsonConverter(typeof(DateRangeAggregationConverter))]
public sealed partial class DateRangeAggregation : SearchAggregation
{
	public DateRangeAggregation(string name) => Name = name;

	internal DateRangeAggregation()
	{
	}

	public Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? Aggregations { get; set; }

	/// <summary>
	/// <para>The date field whose values are use to build ranges.</para>
	/// </summary>
	public Elastic.Clients.Elasticsearch.Serverless.Field? Field { get; set; }

	/// <summary>
	/// <para>The date format used to format `from` and `to` in the response.</para>
	/// </summary>
	public string? Format { get; set; }
	public IDictionary<string, object>? Meta { get; set; }

	/// <summary>
	/// <para>The value to apply to documents that do not have a value.<br/>By default, documents without a value are ignored.</para>
	/// </summary>
	public FieldValue? Missing { get; set; }
	override public string? Name { get; internal set; }

	/// <summary>
	/// <para>Array of date ranges.</para>
	/// </summary>
	public ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeExpression>? Ranges { get; set; }

	/// <summary>
	/// <para>Time zone used to convert dates from another time zone to UTC.</para>
	/// </summary>
	public string? TimeZone { get; set; }
}

public sealed partial class DateRangeAggregationDescriptor<TDocument> : SerializableDescriptor<DateRangeAggregationDescriptor<TDocument>>
{
	internal DateRangeAggregationDescriptor(Action<DateRangeAggregationDescriptor<TDocument>> configure) => configure.Invoke(this);

	public DateRangeAggregationDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? AggregationsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument> AggregationsDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument>> AggregationsDescriptorAction { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Field? FieldValue { get; set; }
	private string? FormatValue { get; set; }
	private IDictionary<string, object>? MetaValue { get; set; }
	private FieldValue? MissingValue { get; set; }
	private ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeExpression>? RangesValue { get; set; }
	private DateRangeExpressionDescriptor RangesDescriptor { get; set; }
	private Action<DateRangeExpressionDescriptor> RangesDescriptorAction { get; set; }
	private Action<DateRangeExpressionDescriptor>[] RangesDescriptorActions { get; set; }
	private string? TimeZoneValue { get; set; }

	public DateRangeAggregationDescriptor<TDocument> Aggregations(Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? aggregations)
	{
		AggregationsDescriptor = null;
		AggregationsDescriptorAction = null;
		AggregationsValue = aggregations;
		return Self;
	}

	public DateRangeAggregationDescriptor<TDocument> Aggregations(Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument> descriptor)
	{
		AggregationsValue = null;
		AggregationsDescriptorAction = null;
		AggregationsDescriptor = descriptor;
		return Self;
	}

	public DateRangeAggregationDescriptor<TDocument> Aggregations(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor<TDocument>> configure)
	{
		AggregationsValue = null;
		AggregationsDescriptor = null;
		AggregationsDescriptorAction = configure;
		return Self;
	}

	/// <summary>
	/// <para>The date field whose values are use to build ranges.</para>
	/// </summary>
	public DateRangeAggregationDescriptor<TDocument> Field(Elastic.Clients.Elasticsearch.Serverless.Field? field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>The date field whose values are use to build ranges.</para>
	/// </summary>
	public DateRangeAggregationDescriptor<TDocument> Field<TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>The date format used to format `from` and `to` in the response.</para>
	/// </summary>
	public DateRangeAggregationDescriptor<TDocument> Format(string? format)
	{
		FormatValue = format;
		return Self;
	}

	public DateRangeAggregationDescriptor<TDocument> Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
	{
		MetaValue = selector?.Invoke(new FluentDictionary<string, object>());
		return Self;
	}

	/// <summary>
	/// <para>The value to apply to documents that do not have a value.<br/>By default, documents without a value are ignored.</para>
	/// </summary>
	public DateRangeAggregationDescriptor<TDocument> Missing(FieldValue? missing)
	{
		MissingValue = missing;
		return Self;
	}

	/// <summary>
	/// <para>Array of date ranges.</para>
	/// </summary>
	public DateRangeAggregationDescriptor<TDocument> Ranges(ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeExpression>? ranges)
	{
		RangesDescriptor = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = null;
		RangesValue = ranges;
		return Self;
	}

	public DateRangeAggregationDescriptor<TDocument> Ranges(DateRangeExpressionDescriptor descriptor)
	{
		RangesValue = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = null;
		RangesDescriptor = descriptor;
		return Self;
	}

	public DateRangeAggregationDescriptor<TDocument> Ranges(Action<DateRangeExpressionDescriptor> configure)
	{
		RangesValue = null;
		RangesDescriptor = null;
		RangesDescriptorActions = null;
		RangesDescriptorAction = configure;
		return Self;
	}

	public DateRangeAggregationDescriptor<TDocument> Ranges(params Action<DateRangeExpressionDescriptor>[] configure)
	{
		RangesValue = null;
		RangesDescriptor = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = configure;
		return Self;
	}

	/// <summary>
	/// <para>Time zone used to convert dates from another time zone to UTC.</para>
	/// </summary>
	public DateRangeAggregationDescriptor<TDocument> TimeZone(string? timeZone)
	{
		TimeZoneValue = timeZone;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("date_range");
		writer.WriteStartObject();
		if (FieldValue is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, FieldValue, options);
		}

		if (!string.IsNullOrEmpty(FormatValue))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(FormatValue);
		}

		if (MissingValue is not null)
		{
			writer.WritePropertyName("missing");
			JsonSerializer.Serialize(writer, MissingValue, options);
		}

		if (RangesDescriptor is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, RangesDescriptor, options);
			writer.WriteEndArray();
		}
		else if (RangesDescriptorAction is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new DateRangeExpressionDescriptor(RangesDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (RangesDescriptorActions is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			foreach (var action in RangesDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new DateRangeExpressionDescriptor(action), options);
			}

			writer.WriteEndArray();
		}
		else if (RangesValue is not null)
		{
			writer.WritePropertyName("ranges");
			JsonSerializer.Serialize(writer, RangesValue, options);
		}

		if (TimeZoneValue is not null)
		{
			writer.WritePropertyName("time_zone");
			JsonSerializer.Serialize(writer, TimeZoneValue, options);
		}

		writer.WriteEndObject();
		if (MetaValue is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, MetaValue, options);
		}

		if (AggregationsDescriptor is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsDescriptor, options);
		}
		else if (AggregationsDescriptorAction is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, new AggregationDescriptor<TDocument>(AggregationsDescriptorAction), options);
		}
		else if (AggregationsValue is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsValue, options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class DateRangeAggregationDescriptor : SerializableDescriptor<DateRangeAggregationDescriptor>
{
	internal DateRangeAggregationDescriptor(Action<DateRangeAggregationDescriptor> configure) => configure.Invoke(this);

	public DateRangeAggregationDescriptor() : base()
	{
	}

	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? AggregationsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor AggregationsDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor> AggregationsDescriptorAction { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.Field? FieldValue { get; set; }
	private string? FormatValue { get; set; }
	private IDictionary<string, object>? MetaValue { get; set; }
	private FieldValue? MissingValue { get; set; }
	private ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeExpression>? RangesValue { get; set; }
	private DateRangeExpressionDescriptor RangesDescriptor { get; set; }
	private Action<DateRangeExpressionDescriptor> RangesDescriptorAction { get; set; }
	private Action<DateRangeExpressionDescriptor>[] RangesDescriptorActions { get; set; }
	private string? TimeZoneValue { get; set; }

	public DateRangeAggregationDescriptor Aggregations(Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDictionary? aggregations)
	{
		AggregationsDescriptor = null;
		AggregationsDescriptorAction = null;
		AggregationsValue = aggregations;
		return Self;
	}

	public DateRangeAggregationDescriptor Aggregations(Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor descriptor)
	{
		AggregationsValue = null;
		AggregationsDescriptorAction = null;
		AggregationsDescriptor = descriptor;
		return Self;
	}

	public DateRangeAggregationDescriptor Aggregations(Action<Elastic.Clients.Elasticsearch.Serverless.Aggregations.AggregationDescriptor> configure)
	{
		AggregationsValue = null;
		AggregationsDescriptor = null;
		AggregationsDescriptorAction = configure;
		return Self;
	}

	/// <summary>
	/// <para>The date field whose values are use to build ranges.</para>
	/// </summary>
	public DateRangeAggregationDescriptor Field(Elastic.Clients.Elasticsearch.Serverless.Field? field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>The date field whose values are use to build ranges.</para>
	/// </summary>
	public DateRangeAggregationDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>The date field whose values are use to build ranges.</para>
	/// </summary>
	public DateRangeAggregationDescriptor Field<TDocument>(Expression<Func<TDocument, object>> field)
	{
		FieldValue = field;
		return Self;
	}

	/// <summary>
	/// <para>The date format used to format `from` and `to` in the response.</para>
	/// </summary>
	public DateRangeAggregationDescriptor Format(string? format)
	{
		FormatValue = format;
		return Self;
	}

	public DateRangeAggregationDescriptor Meta(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
	{
		MetaValue = selector?.Invoke(new FluentDictionary<string, object>());
		return Self;
	}

	/// <summary>
	/// <para>The value to apply to documents that do not have a value.<br/>By default, documents without a value are ignored.</para>
	/// </summary>
	public DateRangeAggregationDescriptor Missing(FieldValue? missing)
	{
		MissingValue = missing;
		return Self;
	}

	/// <summary>
	/// <para>Array of date ranges.</para>
	/// </summary>
	public DateRangeAggregationDescriptor Ranges(ICollection<Elastic.Clients.Elasticsearch.Serverless.Aggregations.DateRangeExpression>? ranges)
	{
		RangesDescriptor = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = null;
		RangesValue = ranges;
		return Self;
	}

	public DateRangeAggregationDescriptor Ranges(DateRangeExpressionDescriptor descriptor)
	{
		RangesValue = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = null;
		RangesDescriptor = descriptor;
		return Self;
	}

	public DateRangeAggregationDescriptor Ranges(Action<DateRangeExpressionDescriptor> configure)
	{
		RangesValue = null;
		RangesDescriptor = null;
		RangesDescriptorActions = null;
		RangesDescriptorAction = configure;
		return Self;
	}

	public DateRangeAggregationDescriptor Ranges(params Action<DateRangeExpressionDescriptor>[] configure)
	{
		RangesValue = null;
		RangesDescriptor = null;
		RangesDescriptorAction = null;
		RangesDescriptorActions = configure;
		return Self;
	}

	/// <summary>
	/// <para>Time zone used to convert dates from another time zone to UTC.</para>
	/// </summary>
	public DateRangeAggregationDescriptor TimeZone(string? timeZone)
	{
		TimeZoneValue = timeZone;
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		writer.WritePropertyName("date_range");
		writer.WriteStartObject();
		if (FieldValue is not null)
		{
			writer.WritePropertyName("field");
			JsonSerializer.Serialize(writer, FieldValue, options);
		}

		if (!string.IsNullOrEmpty(FormatValue))
		{
			writer.WritePropertyName("format");
			writer.WriteStringValue(FormatValue);
		}

		if (MissingValue is not null)
		{
			writer.WritePropertyName("missing");
			JsonSerializer.Serialize(writer, MissingValue, options);
		}

		if (RangesDescriptor is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, RangesDescriptor, options);
			writer.WriteEndArray();
		}
		else if (RangesDescriptorAction is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new DateRangeExpressionDescriptor(RangesDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (RangesDescriptorActions is not null)
		{
			writer.WritePropertyName("ranges");
			writer.WriteStartArray();
			foreach (var action in RangesDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new DateRangeExpressionDescriptor(action), options);
			}

			writer.WriteEndArray();
		}
		else if (RangesValue is not null)
		{
			writer.WritePropertyName("ranges");
			JsonSerializer.Serialize(writer, RangesValue, options);
		}

		if (TimeZoneValue is not null)
		{
			writer.WritePropertyName("time_zone");
			JsonSerializer.Serialize(writer, TimeZoneValue, options);
		}

		writer.WriteEndObject();
		if (MetaValue is not null)
		{
			writer.WritePropertyName("meta");
			JsonSerializer.Serialize(writer, MetaValue, options);
		}

		if (AggregationsDescriptor is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsDescriptor, options);
		}
		else if (AggregationsDescriptorAction is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, new AggregationDescriptor(AggregationsDescriptorAction), options);
		}
		else if (AggregationsValue is not null)
		{
			writer.WritePropertyName("aggregations");
			JsonSerializer.Serialize(writer, AggregationsValue, options);
		}

		writer.WriteEndObject();
	}
}